package com.ilxqx.fierce.core;

import com.ilxqx.fierce.exception.FierceRequestException;

/**
 * 请求上下文Holder
 *
 * @author Venus
 * @since 1.0.0
 */
public final class FierceRequestContextHolder {

    private static final ThreadLocal<FierceRequestContext> FIERCE_REQUEST_CONTEXT_HOLDER = new ThreadLocal<>();

    public static FierceRequestContext get() {
        FierceRequestContext context = FIERCE_REQUEST_CONTEXT_HOLDER.get();
        if (context == null) {
            throw new FierceRequestException("FierceRequestContext must not be null before request handling.");
        }
        return context;
    }

    static void set(FierceRequestContext context) {
        FIERCE_REQUEST_CONTEXT_HOLDER.set(context);
    }

    static void clear() {
        FIERCE_REQUEST_CONTEXT_HOLDER.remove();
    }
}
